package com.woniuxy.oldmanapi.controller;

import com.woniuxy.oldmanapi.entity.Room;
import com.woniuxy.oldmanapi.service.RoomService;
import com.woniuxy.oldmanapi.vo.ResponseResult;
import com.woniuxy.oldmanapi.vo.RoomVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class RoomController {

    @Autowired
    RoomService roomService;

    @GetMapping("/room/list/{id}")
    public ResponseResult<List<RoomVo>> list(@PathVariable("id") int id) {
        ResponseResult<List<RoomVo>> result = new ResponseResult<>();
        try {
            List<RoomVo> lists = roomService.findRooms(id);
            result.setCode(200);
            result.setMsg("查询成功!");
            result.setData(lists);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("查询失败!");
        }
        return result;
    }
    @GetMapping("/room/num/{id}")
    public ResponseResult<Integer> num(@PathVariable("id") int id) {
        ResponseResult<Integer> result = new ResponseResult<>();
        try {
            int a = roomService.findRoomNum(id);
            result.setCode(200);
            result.setMsg("查数成功!");
            result.setData(a);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("查数失败!");
        }
        return  result;
    }

    @GetMapping("/room/selectByStoreyIdYSW/{buildingId}/{storeyId}")
    public ResponseResult<List<Room>> selectByStoreyIdYSW(@PathVariable("buildingId") Integer buildingId, @PathVariable("storeyId") Integer storeyId) {
        ResponseResult<List<Room>> result = new ResponseResult<>();
        try {
            List<Room> list = roomService.selectByStoreyIdYSW(buildingId, storeyId);
            if (list != null) {
                result.setCode(200);
                result.setData(list);
                result.setMsg("查询成功");
            } else {
                result.setCode(204);
                result.setMsg("该楼层未录入房间");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.setMsg("系统繁忙,请稍后再试");
            result.setCode(205);

        }
        return result;
    }

    @PostMapping("/room/add")
    public ResponseResult<Void> add(@RequestBody Room room) {
        ResponseResult<Void> result = new ResponseResult<>();
        try {
            roomService.add(room);
            result.setCode(200);
            result.setMsg("添加成功!");
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("添加失败!");
        }
        return result;
    }

    @PutMapping("/room/modify")
    public ResponseResult<Void> modify(@RequestBody Room room) {
        ResponseResult<Void> result = new ResponseResult<>();
        try {
            roomService.modify(room);
            result.setCode(200);
            result.setMsg("修改成功!");
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("修改失败!");
        }
        return result;
    }

    @DeleteMapping("/room/delete/{id}")
    public ResponseResult<Void> delete(@PathVariable("id")int id) {
        ResponseResult<Void> result = new ResponseResult<>();
        try {
            roomService.delete(id);
            result.setCode(200);
            result.setMsg("删除成功!");
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("删除失败!");
        }
        return result;
    }

    @GetMapping("/room/one/{id}")
    public ResponseResult<Room> one(@PathVariable("id") int id) {
        ResponseResult<Room> result = new ResponseResult<>();
        try {
            Room room = roomService.selectOne(id);
            result.setCode(200);
            result.setMsg("查询成功!");
            result.setData(room);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("查询失败!");
        }
        return result;
    }

    @GetMapping("/room/selectAllList")
    public ResponseResult<List<Room>> selectAllList() {
        ResponseResult<List<Room>> result = new ResponseResult<>();
        try {
            List<Room> list = roomService.selectAllListYsw();
            result.setCode(200);
            result.setData(list);
            result.setMsg("查询成功");
        } catch (Exception e) {
            e.printStackTrace();
            result.setMsg("系统繁忙,请稍后再试");
            result.setCode(205);

        }
        return result;
    }
}
